排序 插入、希尔、冒泡、选择
def insertSort(arr):
for i in range(1,len(arr)):
tmp = arr[i]
for j in range(i-1,-1,-1):
if arr[j] <= tmp:
arr[j+1] = tmp
break
else:
arr[j+1] = arr[j]
return arr
def shellSort(arr):
d = len(arr)//2
while d>=1:
for i in range(d,len(arr)):
tmp = arr[i]
for j in range(i-d,-1,-1):
if arr[j] <= tmp:
arr[j+d] = tmp
break
else:
arr[j+d] = arr[j]
d=d//2
return arr
def bubbleSort(arr):
for i in range(len(arr)):
for j in range(i+1,len(arr)):
if arr[i] > arr[j]:
arr[i],arr[j] = arr[j],arr[i]
return arr
def selectSort(arr):
for i in range(len(arr)-2):
min_ = arr[i]
min_i = i
for j in range(i+1,len(arr)):
if arr[j] < min_:
min_ = arr[j]
min_i = j
arr[i],arr[min_i] = arr[min_i],arr[i]
return arr
排序 快速排序及应用
def QuickSort(arr,start,end,k=6):
if start >= end:
return
i,j = start,end
base = arr[i]
while i < j:
while i < j and arr[j] >= base:
j -=1
arr[i] = arr[j]
while i < j and arr[i] <= base:
i +=1
arr[j] = arr[i]
arr[i] = base
QuickSort(arr, start, i - 1)
QuickSort(arr, j + 1, end)
#应用 找乱序数组中第K大的数字(也可用最大堆) o(nlogm):
def FindKth(arr,start,end,k=6):
if start >= end:
return
i,j = start,end
base = arr[i]
while i < j:
while i < j and arr[j] >= base:
j -=1
arr[i] = arr[j]
while i < j and arr[i] <= base:
i +=1
arr[j] = arr[i]
arr[i] = base
#此时 i==j
if j == k:
return
elif k<j: #要找的第K大数在左半边
FindKth(arr, start, i - 1)
elif k>j:
FindKth(arr, j + 1, end)
return arr[k]
归并排序
#归并排序模板
def merge(nums,left,right):
mid = (left+right)//2
if left<right:
merge(nums,left,mid)
merge(nums,mid+1,right)
mergeSort(nums,left,mid,right)
def mergeSort(nums,left,mid,right):#[left,mid][mid+1,right]
i=left;j=mid+1
temp = []
while i<=mid and j<= right:
if nums[i]<nums[j]:
temp.append(nums[i])
i+=1
else:
temp.append(nums[j])
j+=1
if i<= mid:
temp.extend(nums[i:mid+1])
if j<=right:
temp.extend(nums[j:right+1])
for i in range(len(temp)):
nums[left+i] = temp[i]
并查集模板
#init
n = 6
parent = [-1]*n
rank = [0]*n
edges = [(0,1),(1,2),(2,3),(1,4),(2,5)]
def find(x,parent):
while parent[x] != -1:
x = parent[x]
return x
def union(x,y,parent,rank):
x_root = find(x,parent)
y_root = find(y,parent)
if x_root == y_root:
return 0
else:
if rank[x_root] > rank[y_root]:
parent[y_root] = x_root
elif rank[x_root] < rank[y_root]:
parent[x_root] = y_root
else:
parent[x_root] = y_root
rank[y_root] += 1
return 1
for i in range(n-1):
if union(edges[i][0],edges[i][1],parent,rank) == 0:
print('有环')
else:
print('五环')
二分查找模板(check需要自己完成 返回true or false)
def bsearch(nums):
l = 0;r=len(nums)-1
while l<r:
mid = (l+r)>>1#[l,mid][mid+1,r]
if check(mid):
r = mid
else:
l = mid + 1
def bserach(nums):
l = 0;r=len(nums)-1
while l<r:
mid = (l+r+1)>>1#[l,mid-1][mid,r]
if check(mid):
r = mid-1
else:
l = mid
堆排序模板
def heapify(arr, n, i):
largest = i
l = 2 * i + 1 # left = 2*i + 1
r = 2 * i + 2 # right = 2*i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
if largest != i:
arr[i],arr[largest] = arr[largest],arr[i] # 交换
heapify(arr, n, largest)
def heapSort(arr):
n = len(arr)
# Build a maxheap.
for i in range(n, -1, -1):
heapify(arr, n, i)
# 一个个交换元素
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # 交换
heapify(arr, i, 0)
arr = [ 12, 11, 13, 5, 6, 7]
heapSort(arr)
n = len(arr)
print ("排序后")
for i in range(n):
print ("%d" %arr[i])
桶排序
def RadixSort(a):
i = 0 #初始为个位排序
n = 1 #最小的位数置为1(包含0)
max_num = max(a) #得到带排序数组中最大数
while max_num > 10**n: #得到最大数是几位数
n += 1
while i < n:
bucket = {} #用字典构建桶
for x in range(10):
bucket.setdefault(x, []) #将每个桶置空
for x in a: #对每一位进行排序
radix =int((x / (10**i)) % 10) #得到每位的基数
bucket[radix].append(x) #将对应的数组元素加入到相应位基数的桶中
j = 0
for k in range(10):
if len(bucket[k]) != 0: #若桶不为空
for y in bucket[k]: #将该桶中每个元素
a[j] = y #放回到数组中
j += 1
i += 1
arr = [12,3,45,3543,214,1,4553]
RadixSort(arr)
print(arr)
回溯
排列组合(含重复)
import copy
#A(N,N) n个数放n个坑 放的方法
#方法1
def permuteUnique(nums):
n = len(nums)
res = []
res.clear()
#将重复元素放一块儿,usd>>(i-1)就能检查重复
nums.sort()
def traceback(tmp,usd):
if len(tmp) == n:
res.append(copy.deepcopy(tmp))
elif len(tmp)>n:
return
for i in range(n):
#当出现重复如[0,1,2,2,2]
#不妨记为[0,1,a,b,c]
#[abc]全排列6种只能保留1种,如[0,1,c,b,a]
#usd>>(i-1)&1 == 0 代表之前没用过 [acb]中,遇到b时,a已经用过不行
if i > 0 and nums[i] == nums[i-1] and usd>>(i-1)&1 == 0:
continue
#usd记录使用过元素,一个元素不能放两遍
if usd>>i&1 ==0:
traceback(tmp+[nums[i]],usd+(1<<i))
traceback([],0)
return res
#方法2
#[0,1,2,2,3]线路x
#[-,1,2,2,3]线路x -表示位置确定的第一个选到的放第一个位置
#[-,1,2,2,-]线路x
#线路x分支1->[-,1,2,-,-]
#线路x分支2->[-,1,-,2,-]#与分支1重复,去除
def permuteUnique(nums):
nums.sort()
res = []
def backtrack(nums, tmp):
if not nums:
res.append(tmp)
return
for i in range(len(nums)):
if i > 0 and nums[i] == nums[i - 1]:
continue
backtrack(nums[:i] + nums[i + 1:], tmp + [nums[i]])
backtrack(nums, [])
return res
#组合
import copy
def combinationSum(collections,target):
res = []
res.clear()
collections.sort()
def backtrace(tmp,index,_sum):
if _sum == target:
res.append(copy.deepcopy(tmp))
elif _sum>target:
return
for i in range(index,len(collections)):
if i>index and collections[i] == collections[i-1]:
continue
elif _sum + collections[i] > target:
break
backtrace(tmp+[collections[i]],i+1,_sum+collections[i])
backtrace([],0,0)
return res
8皇后
def solveNQueens(n):
res = []
if n == 0:
return res
nums = [i for i in range(n)]
col = []
master = []
slave = []
stack = []
def __convert2board(stack):
return ["." * stack[i] + "Q" + "." * (n - stack[i] - 1) for i in range(n)]
def backtrace(nums, row, col, master, slave, stack):
if row == n:
board = __convert2board(stack)
res.append(copy.deepcopy(board))
return
for i in range(n):
#col已放置过的列 #副对角线 b[i]+i = b[j]+j #主对角线
#row当前行
if i not in col and row + i not in master and row - i not in slave:
stack += [nums[i]]
col += [i]
master += [row + i]
slave += [row - i]
backtrace(nums, row + 1, col, master, slave, stack)
slave.pop()
master.pop()
col.pop()
stack.pop()
backtrace(nums, 0, col, master, slave, stack)
return res
快速幂
def qpow(x,n):
res = 1
while n:
if n&1:
res*=x
x*=x
n>>=1
return res
下一个排列
def nextPermutation(nums):
#[1,2,7,4,3,1]
#[1,(2),(7),4,3,1]
#[1,2,7,4,(3),1]
#[1,(3),7,4,(2),1]
#[1,3,1,2,4,7]
def reverse(nums, i, j):
while i < j:
nums[i],nums[j] = nums[j], nums[i]
i += 1
j -= 1
n = len(nums)
k0,k1 = -1,-1
for i in range(n-2,-1,-1):
if nums[i] < nums[i+1]:
k0 = i
break
if k0 == -1:
reverse(nums,0,len(nums)-1)
return
for i in range(n-1,-1,-1):
if nums[i] > nums[k0]:
k1 = i
break
if k1 == -1:
reverse(nums,0,len(nums)-1)
return
nums[k0],nums[k1] = nums[k1],nums[k0]
nums[k0+1:]=nums[k0+1:][::-1]
return
二叉树遍历
#层次遍历
def levelOrder(root):
res = []
def pre(root,depth):
if not root:
return
if depth >= len(res):
res.append([])
res[depth].append(root.val)
pre(root.left,depth+1)
pre(root.right,depth+1)
pre(root,0)
return res
区间重合
#252 会议室 1
def canAttendMeetings(intervals):
intervals.sort();
for i in range(1,len(intervals)):
if intervals[i][0] < intervals[i-1][1]:
return False
return True
#253 会议室 2
def canAttendMeetings2(intervals):
hash = collections.defaultdict(int)
res = 0
acc = 0
for x in intervals:
hash[x[0]]+=1
hash[x[1]]-=1
items = sorted(hash.items(),key = lambda x:(x[0],-x[1]))
for k,v in items:
acc += v
res = max(res,acc)
return res
import heapq
def minMeetingRooms2(intervals):
"""
:type intervals: List[List[int]]
:rtype: int
"""
if not intervals:
return 0
intervals.sort(key = lambda i: i[0])
heap = [intervals[0][1]]
for i in range(1,len(intervals)):
if intervals[i][0] >= heap[0]:
heapq.heappop(heap)
heapq.heappush(heap, intervals[i][1])
else:
heapq.heappush(heap, intervals[i][1])
return len(heap)
#1094:拼车
def canAttendMeetings3(intervals,cap):
hash = collections.defaultdict(int)
res = 0
acc = 0
for x in intervals:
hash[x[1]]+=x[0]
hash[x[2]]-=x[0]
items = sorted(hash.items(),key = lambda x:(x[0],-x[1]))
for k,v in items:
acc += v
res = max(res,acc)
if res > cap:
return False
return True
trie
#208
class Trie:
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = {}
def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
cur = self.root
for c in word:
if c not in cur:
cur[c] = {}
cur = cur[c]
cur['#'] = True
def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
cur = self.root
for c in word:
if cur.get(c,None)!=None:
cur = cur[c]
else:
return False
return '#' in cur
def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
cur = self.root
for c in prefix:
if cur.get(c,None)!=None:
cur = cur[c]
else:
return False
return True
旋转二分
#81
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。